home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 8.4 KB | 351 lines | [TEXT/MPS ] |
- /*
- File: VirtualFolder.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __VIRTUALFOLDER__
- #include "VirtualFolder.h"
- #endif
-
- #ifndef __VIRTUALHSFMACFILE__
- #include "VirtualHFSMacFile.h"
- #endif
-
- #ifndef __DEBUGGINGEAR__
- #include "DebuggingGear.h"
- #endif
-
- /***********************************|****************************************/
-
- #pragma segment VirtualFolder
-
- /***********************************|****************************************/
-
- ImplementList ( TVirtualMacFile, TMacFileList, true );
- ImplementList ( TVirtualFolder, TFolderList, true );
-
- /***********************************|****************************************/
-
- TVirtualFolder::TVirtualFolder(const FSSpec& spec, Boolean mimicActualHFS ):
- THandleObject (),
- fSpec ( spec ),
- fFileList ( new TMacFileList ),
- fFolderList ( new TFolderList )
- {
- if ( mimicActualHFS )
- {
- TRY
- {
- InheritDown ( spec );
- }
- EXCEPTION
- {
- }
- ENDEXCEPTION
- }
- }
-
- /***********************************|****************************************/
-
- TVirtualFolder::TVirtualFolder(const Str255 name):
- THandleObject (),
- fFileList ( new TMacFileList ),
- fFolderList ( new TFolderList )
- {
- fSpec.vRefNum = -1;
- fSpec.parID = 2;
- SetFolderName(name);
- }
-
- /***********************************|****************************************/
-
- TVirtualFolder::TVirtualFolder(const char* fileName) :
- THandleObject (),
- fFileList ( new TMacFileList ),
- fFolderList ( new TFolderList )
- {
- fSpec.vRefNum = -1;
- fSpec.parID = 2;
-
- SetFolderName( fileName );
- }
-
- /***********************************|****************************************/
-
- TVirtualFolder::~TVirtualFolder()
- {
- #if debug
- if (steveFlag.Flag(18))
- steve << "TVirtualFolder::~TVirtualFolder, @ " << (long) this << ", deleting " << FileCount() << " sub-files, " << FolderCount() << " sub-folders of folder." << endl;
- #endif
-
- delete fFileList;
- delete fFolderList;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualFolder::AddFile(TVirtualMacFile* file)
- {
- if ( file )
- {
- Str31 name;
- file->GetFileName(name);
-
- if (IsFileFolderNameUnique(name))
- {
- fFileList->Append(file);
- return noErr;
- }
- else
- return dupFNErr;
- }
- else
- return nilHandleErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualFolder::AddFolder(TVirtualFolder* folder)
- {
- if ( folder )
- {
- Str31 name;
- folder->GetFolderName(name);
-
- if (IsFileFolderNameUnique(name))
- {
- fFolderList->Append(folder);
- return noErr;
- }
- else
- return dupFNErr;
- }
- else
- return nilHandleErr;
- }
-
- /***********************************|****************************************/
-
- Boolean TVirtualFolder::IsFileFolderNameUnique(const StringPtr theName) const
- {
- Str31 itemName;
- unsigned long index, count;
-
- for ( index = 1, count = FileCount(); index <= count; index++ )
- {
- GetIndexFile(index)->GetFileName(itemName);
-
- if (PLstrcmp(itemName, theName) == 0)
- return false;
- }
-
- for (index = 1, count = FolderCount(); index<= count; index++)
- {
- GetIndexFolder(index)->GetFolderName(itemName);
-
- if (PLstrcmp(itemName, theName) == 0)
- return false;
- }
-
- return true;
- }
-
- /***********************************|****************************************/
-
- unsigned long TVirtualFolder::TotalSize() const
- {
- long logEofDF, logEofRF;
- unsigned long index, count, tSize = 0;
-
- for ( index = 1, count = FileCount(); index <= count; index++)
- {
- const TVirtualMacFile* theFile = fFileList->Get(index);
- theFile->GetEnd (logEofDF,TVirtualMacFile::kData);
- theFile->GetEnd (logEofRF,TVirtualMacFile::kResource);
- tSize += logEofRF + logEofDF;
- }
-
- for (index = 1, count = FolderCount(); index <= count; index++)
- tSize += fFolderList->Get(index)->TotalSize();
-
- return tSize;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualFolder::WriteToDisk ( short vRefNum, long parID )
- {
- fSpec.vRefNum = vRefNum;
- fSpec.parID = parID;
- long myDirID = 0;
-
- OSErr error = FSpDirCreate ( &fSpec, smRoman, &myDirID );
-
- if ( error == noErr || error == dupFNErr )
- error = WriteContentsToDisk ( vRefNum, myDirID );
-
- return error;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualFolder::WriteContentsToDisk ( short vRefNum, long parID )
- {
- OSErr error = noErr;
- unsigned long index, count;
- FSSpec spec;
- spec.vRefNum = vRefNum;
- spec.parID = parID;
-
- for ( index = 1, count = FileCount (); index <= count && error == noErr; index++ )
- {
- TVirtualMacFile* file = GetIndexFile ( index );
- file->GetFileName ( spec.name );
- error = file->WriteToDisk ( spec );
- }
-
- for ( index = 1, count = FolderCount (); index <= count && error == noErr; index++ )
- {
- TVirtualFolder* folder = GetIndexFolder ( index );
- error = folder->WriteToDisk ( vRefNum, parID );
- }
-
- return error;
- }
-
- /***********************************|****************************************/
-
- void TVirtualFolder::SetFolderName(const Str255 name)
- {
- PLstrcpy(fSpec.name,name);
- }
-
- /***********************************|****************************************/
-
- void TVirtualFolder::SetFolderName(const char* name)
- {
- strcpy((char*) &fSpec.name[1], name);
- fSpec.name[0] = strlen(name);
- }
-
- /***********************************|****************************************/
-
- void TVirtualFolder::GetFolderName(StringPtr name) const
- {
- PLstrcpy(name,fSpec.name);
- }
-
- /***********************************|****************************************/
-
- void TVirtualFolder::InheritDown ( const FSSpec& fileSpec )
- {
- CInfoPBRec paramBlock;
- short fileIndex = 0;
-
- paramBlock.dirInfo.ioVRefNum = fileSpec.vRefNum;
- paramBlock.dirInfo.ioDrDirID = fileSpec.parID;
- paramBlock.dirInfo.ioFDirIndex = fileIndex;
- paramBlock.dirInfo.ioNamePtr = fileSpec.name;
- OSErr error = PBGetCatInfo(¶mBlock, false);
- FAILOSErr ( error );
-
- long folderRefNum = paramBlock.dirInfo.ioDrDirID; // Get Folder RefNum
-
- while ( !error )
- {
- FSSpec newSpec;
- paramBlock.dirInfo.ioVRefNum = fileSpec.vRefNum;
- paramBlock.dirInfo.ioDrDirID = folderRefNum;
- paramBlock.dirInfo.ioFDirIndex = ++fileIndex;
- paramBlock.dirInfo.ioNamePtr = newSpec.name;
- error = PBGetCatInfo ( ¶mBlock, false );
-
- if ( !error && !BitAnd ( paramBlock.dirInfo.ioDrUsrWds.frFlags, fInvisible ) )
- {
- newSpec.vRefNum = paramBlock.dirInfo.ioVRefNum;
- newSpec.parID = folderRefNum;
-
- if ( BitTst ( ¶mBlock.dirInfo.ioFlAttrib, 3 ) )
- AddFolder ( new TVirtualFolder ( newSpec ) );
- else
- AddFile ( new TVirtualHFSMacFile ( newSpec ) );
- }
- }
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualFolder::SetFinderInfo (const FInfo& finderInfo)
- {
- return ::FSpSetFInfo ( &fSpec, &finderInfo );
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualFolder::GetFinderInfo (FInfo& finderInfo) const
- {
- return ::FSpGetFInfo ( &((TVirtualFolder*)this)->fSpec, &finderInfo );
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualFolder::GetDate (unsigned long& dateTime,TVirtualMacFile::WhichDateType whichDate) const
- {
- CInfoPBRec paramBlock;
-
- paramBlock.dirInfo.ioVRefNum = fSpec.vRefNum;
- paramBlock.dirInfo.ioDrParID = fSpec.parID;
- paramBlock.dirInfo.ioDrDirID = fSpec.parID;
- paramBlock.dirInfo.ioFDirIndex = 0;
- paramBlock.dirInfo.ioNamePtr = fSpec.name;
- OSErr error = PBGetCatInfo(¶mBlock, false);
-
- if (whichDate == TVirtualMacFile::kCreationDate)
- dateTime = paramBlock.dirInfo.ioDrCrDat;
- else
- dateTime = paramBlock.dirInfo.ioDrMdDat;
-
- return error;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualFolder::SetDate (unsigned long dateTime,TVirtualMacFile::WhichDateType whichDate)
- {
- CInfoPBRec paramBlock;
-
- paramBlock.dirInfo.ioVRefNum = fSpec.vRefNum;
- paramBlock.dirInfo.ioDrParID = fSpec.parID;
- paramBlock.dirInfo.ioDrDirID = fSpec.parID;
- paramBlock.dirInfo.ioFDirIndex = 0;
- paramBlock.dirInfo.ioNamePtr = fSpec.name;
- OSErr error = PBGetCatInfo(¶mBlock, false);
-
- if (whichDate == TVirtualMacFile::kCreationDate)
- paramBlock.dirInfo.ioDrCrDat = dateTime;
- else
- paramBlock.dirInfo.ioDrMdDat = dateTime;
-
- paramBlock.dirInfo.ioDrDirID = fSpec.parID;
-
- if (error == noErr)
- error = PBSetCatInfo(¶mBlock, false);
-
- return error;
- }
-
- /***********************************|****************************************/
-